/**
 * 页面路径配置文件
 * 统一管理所有页面路径，便于维护和修改
 * 包含登录检测逻辑和页面跳转工具函数
 */

import { checkLogin, toLogin } from '../libs/login.js';

// ==================== 主要页面 ====================

// 首页相关
export const PAGE_HOME = '/pages/home/home' // 首页（主包）
export const PAGE_INDEX = '/pages/common/index/index' // 索引页
export const PAGE_LOGS = '/pages/logs/logs' // 咨询页（主包）

// 用户相关
export const PAGE_PROFILE = '/pages/profile/profile' // 个人中心（主包）
export const PAGE_LOGIN = '/pages/common/login/login' // 登录页
export const PAGE_PHONE_VERIFICATION = '/pages/common/phone-verification/phone-verification' // 手机验证页

// ==================== 发布相关页面 ====================

export const PAGE_PUBLISH_SELECT = '/pages/publish-select/publish-select' // 发布选择页（主包）
export const PAGE_PUBLISH_DEMAND = '/pages/publish/publish-demand/publish-demand' // 发布需求页
export const PAGE_PUBLISH_GROUP = '/pages/publish/publish-group/publish-group' // 发布拼单页
export const PAGE_PUBLISH_INVENTORY = '/pages/publish/publish-inventory/publish-inventory' // 发布库存页
export const PAGE_PUBLISH_MATERIAL = '/pages/publish/publish-material/publish-material' // 发布材料页
export const PAGE_PUBLISH_PROCESSING = '/pages/publish/publish-processing/publish-processing' // 发布加工页

// ==================== 企业相关页面 ====================

export const PAGE_CREATE_ENTERPRISE = '/pages/enterprise/create-enterprise/create-enterprise' // 创建企业页
export const PAGE_MY_ENTERPRISE = '/pages/enterprise/my-enterprise/my-enterprise' // 我的企业页
export const PAGE_EDIT_COMPANY = '/pages/user/edit-company/edit-company' // 编辑企业页
export const PAGE_MEMBERSHIP_REGISTER = '/pages/enterprise/membership-register/membership-register' // 会员注册页
export const PAGE_COMPANY_PROFILE = '/pages/enterprise/company-profile/company-profile' // 企业详情页
export const PAGE_ENTERPRISE_PRODUCT = '/pages/enterprise/enterprise-product/enterprise-product' // 企业产品页

// ==================== 产品相关页面 ====================

export const PAGE_PRODUCT_HOME = '/pages/product/product-home/product-home' // 产品首页
export const PAGE_PRODUCT_LIST_MATERIAL = '/pages/product/product-list-material/product-list-material' // 材料产品列表页
export const PAGE_PRODUCT_LIST_PROCESSING = '/pages/product/product-list-processing/product-list-processing' // 加工产品列表页
export const PAGE_PRODUCT_DETAIL = '/pages/product/product-detail/product-detail' // 产品详情页

// ==================== 散料相关页面 ====================

export const PAGE_BULK_MATERIALS_HOME = '/pages/bulk/bulk-materials-home/bulk-materials-home' // 散料首页
export const PAGE_BULK_MATERIALS = '/pages/bulk/bulk-materials/bulk-materials' // 散料列表页
export const PAGE_BULK_MATERIAL_DETAIL = '/pages/bulk/bulk-material-detail/bulk-material-detail' // 散料详情页
export const PAGE_BULK_MATERIALS_CATEGORY = '/pages/bulk/bulk-materials-category/bulk-materials-category' // 散料分类页
export const PAGE_BULK_PROCESSING_CATEGORY = '/pages/bulk/bulk-processing-category/bulk-processing-category' // 散料加工分类页
export const PAGE_BULK_INVENTORY_CATEGORY = '/pages/bulk/bulk-inventory-category/bulk-inventory-category' // 散料一览分类页

// ==================== 搜索相关页面 ====================

export const PAGE_SEARCH_RESULTS = '/pages/common/search-results/search-results' // 搜索结果页
export const PAGE_LOOK_FOR_TECH_PARAMS = '/pages/common/look-for-tech-params/look-for-tech-params' // 技术参数查找页

// ==================== 需求相关页面 ====================

export const PAGE_DEMAND_LIST = '/pages/demand-list/demand-list' // 需求列表页（主包）
export const PAGE_DEMAND_DETAIL = '/pages/demand/demand-detail/demand-detail' // 求购详情页
export const PAGE_GROUP_LIST = '/pages/group/group-list/group-list' // 拼单列表页
export const PAGE_GROUP_DETAIL = '/pages/group/group-detail/group-detail' // 拼单详情页

// ==================== 我的相关页面 ====================

// 积分相关
export const PAGE_MY_POINTS = '/pages/user/my-points/my-points' // 我的积分页
export const PAGE_POINTS_RULES = '/pages/user/points-rules/points-rules' // 积分规则页
export const PAGE_PRIZES = '/pages/user/prizes/prizes' // 奖品页（积分商城）
export const PAGE_MY_PRIZES = '/pages/user/my-prizes/my-prizes' // 我的奖品页

// 材料相关
export const PAGE_MY_MATERIALS = '/pages/my-business/my-materials/my-materials' // 我的材料页
export const PAGE_MY_MATERIAL_DETAIL = '/pages/my-business/my-material-detail/my-material-detail' // 我的材料详情页
export const PAGE_MY_MATERIAL_EDIT = '/pages/my-business/my-material-edit/my-material-edit' // 我的材料编辑页

// 散料相关（我的）
export const PAGE_MY_BULK_MATERIALS = '/pages/my-business/my-bulk-materials/my-bulk-materials' // 我的散料页
export const PAGE_MY_BULK_MATERIAL_DETAIL = '/pages/my-business/my-bulk-material-detail/my-bulk-material-detail' // 我的散料详情页
export const PAGE_MY_BULK_MATERIAL_EDIT = '/pages/my-business/my-bulk-material-edit/my-bulk-material-edit' // 我的散料编辑页

// 需求相关
export const PAGE_MY_DEMAND_LIST = '/pages/my-business/my-demand-list/my-demand-list' // 我的需求列表页
export const PAGE_MY_DEMAND_DETAIL = '/pages/my-business/my-demand-detail/my-demand-detail' // 我的需求详情页
export const PAGE_MY_DEMAND_EDIT = '/pages/my-business/my-demand-edit/my-demand-edit' // 我的需求编辑页

// 拼单相关
export const PAGE_MY_GROUP_ORDERS = '/pages/my-business/my-group-orders/my-group-orders' // 我的拼单页
export const PAGE_MY_GROUP_DETAIL = '/pages/my-business/my-group-detail/my-group-detail' // 我的拼单详情页
export const PAGE_MY_GROUP_EDIT = '/pages/my-business/my-group-edit/my-group-edit' // 我的拼单编辑页

// 加工相关
export const PAGE_MY_PROCESSING_DETAIL = '/pages/my-business/my-processing-detail/my-processing-detail' // 我的加工详情页
export const PAGE_MY_PROCESSING_EDIT = '/pages/my-business/my-processing-edit/my-processing-edit' // 我的加工编辑页

// ==================== 其他功能页面 ====================

// 消息和通知
export const PAGE_MESSAGES = '/pages/user/message-list/message-list' // 消息列表页
export const PAGE_MESSAGE_DETAIL = '/pages/user/message-detail/message-detail' // 消息详情页
export const PAGE_NOTIFICATIONS = '/pages/notifications/notifications' // 通知页
export const PAGE_MY_MESSAGES = '/pages/user/my-messages/my-messages' // 我的留言页
export const PAGE_MY_INQUIRIES = '/pages/my-business/my-inquiries/my-inquiries' // 我的询价页
export const PAGE_MY_PROCESSING = '/pages/my-business/my-processing/my-processing' // 我的加工页
export const PAGE_MY_ORDERS = '/pages/my-business/my-orders/my-orders' // 我的接单页

// 设置和管理
export const PAGE_SETTINGS = '/pages/settings/settings' // 设置页
export const PAGE_PROFILE_DETAIL = '/pages/profile-detail/profile-detail' // 个人资料详情页
export const PAGE_ACCOUNT_MANAGEMENT = '/pages/user/account-management/account-management' // 账号管理页
export const PAGE_ENTERPRISE_MANAGEMENT = '/pages/enterprise/enterprise-management/enterprise-management' // 企业管理页
export const PAGE_EDIT_AVATAR = '/pages/user/edit-avatar/edit-avatar' // 修改头像页
export const PAGE_EDIT_NAME = '/pages/user/edit-name/edit-name' // 修改姓名页
export const PAGE_EDIT_PHONE = '/pages/user/edit-phone/edit-phone' // 修改手机号页

// 业务功能
export const PAGE_RECEIVED_ORDERS = '/pages/received-orders/received-orders' // 我的接单页
export const PAGE_COLLECTIONS = '/pages/user/collections/collections' // 我的收藏页
export const PAGE_INVITATIONS = '/pages/user/invitations/invitations' // 我的邀请页
export const PAGE_INVITE_SHARE = '/pages/user/invite-share/invite-share' // 邀请分享页
export const PAGE_POINTS_MALL = PAGE_PRIZES // 积分商城页（使用PRIZES）
export const PAGE_CONSULTATION = '/pages/consultation/consultation' // 咨询页

// 积分商城相关页面
export const PAGE_INTEGRAL_MALL = '/pages/user/prizes/prizes' // 积分商城首页
export const PAGE_INTEGRAL_DETAIL = '/pages/integral/detail/detail' // 积分商品详情
export const PAGE_INTEGRAL_ORDER_CONFIRM = '/pages/integral/order-confirm/order-confirm' // 积分订单确认
export const PAGE_INTEGRAL_ORDERS = '/pages/integral/orders/orders' // 积分订单列表
export const PAGE_INTEGRAL_ORDER_DETAIL = '/pages/integral/order-detail/order-detail' // 积分订单详情
export const PAGE_INTEGRAL_ORDER_EXPRESS = '/pages/integral/order-express/order-express' // 积分订单物流
export const PAGE_MY_EXCHANGES = '/pages/user/my-exchanges/my-exchanges' // 我的兑换页

// 详情页面
export const PAGE_MATERIAL_DETAIL = '/pages/material-detail/material-detail' // 材料详情页
export const PAGE_PROCESSING_DETAIL = '/pages/processing-detail/processing-detail' // 加工详情页

// ==================== 法律和协议页面 ====================

export const PAGE_DISCLAIMER = '/pages/common/disclaimer/disclaimer' // 免责声明页
export const PAGE_PRIVACY_POLICY = '/pages/common/privacy-policy/privacy-policy' // 隐私政策页
export const PAGE_HELP = '/pages/common/help/help' // 使用说明页

// ==================== 页面路径对象（便于批量操作） ====================

export const PAGES = {
  // 主要页面
  HOME: PAGE_HOME,
  INDEX: PAGE_INDEX,
  LOGS: PAGE_LOGS,
  PROFILE: PAGE_PROFILE,
  LOGIN: PAGE_LOGIN,
  PHONE_VERIFICATION: PAGE_PHONE_VERIFICATION,

  // 发布相关
  PUBLISH_SELECT: PAGE_PUBLISH_SELECT,
  PUBLISH_DEMAND: PAGE_PUBLISH_DEMAND,
  PUBLISH_GROUP: PAGE_PUBLISH_GROUP,
  PUBLISH_INVENTORY: PAGE_PUBLISH_INVENTORY,
  PUBLISH_MATERIAL: PAGE_PUBLISH_MATERIAL,
  PUBLISH_PROCESSING: PAGE_PUBLISH_PROCESSING,

  // 企业相关
  CREATE_ENTERPRISE: PAGE_CREATE_ENTERPRISE,
  MY_ENTERPRISE: PAGE_MY_ENTERPRISE,
  EDIT_COMPANY: PAGE_EDIT_COMPANY,
  MEMBERSHIP_REGISTER: PAGE_MEMBERSHIP_REGISTER,
  COMPANY_PROFILE: PAGE_COMPANY_PROFILE,
  ENTERPRISE_PRODUCT: PAGE_ENTERPRISE_PRODUCT,

  // 产品相关
  PRODUCT_HOME: PAGE_PRODUCT_HOME,
  PRODUCT_LIST_MATERIAL: PAGE_PRODUCT_LIST_MATERIAL,
  PRODUCT_LIST_PROCESSING: PAGE_PRODUCT_LIST_PROCESSING,
  PRODUCT_DETAIL: PAGE_PRODUCT_DETAIL,

  // 散料相关
  BULK_MATERIALS_HOME: PAGE_BULK_MATERIALS_HOME,
  BULK_MATERIALS: PAGE_BULK_MATERIALS,
  BULK_MATERIAL_DETAIL: PAGE_BULK_MATERIAL_DETAIL,
  BULK_MATERIALS_CATEGORY: PAGE_BULK_MATERIALS_CATEGORY,
  BULK_PROCESSING_CATEGORY: PAGE_BULK_PROCESSING_CATEGORY,
  BULK_INVENTORY_CATEGORY: PAGE_BULK_INVENTORY_CATEGORY,

  // 搜索相关
  SEARCH_RESULTS: PAGE_SEARCH_RESULTS,
  LOOK_FOR_TECH_PARAMS: PAGE_LOOK_FOR_TECH_PARAMS,

  // 需求相关
  DEMAND_LIST: PAGE_DEMAND_LIST,
  DEMAND_DETAIL: PAGE_DEMAND_DETAIL,
  GROUP_LIST: PAGE_GROUP_LIST,
  GROUP_DETAIL: PAGE_GROUP_DETAIL,

  // 我的相关
  MY_POINTS: PAGE_MY_POINTS,
  POINTS_RULES: PAGE_POINTS_RULES,
  PRIZES: PAGE_PRIZES,
  MY_PRIZES: PAGE_MY_PRIZES,
  MY_MATERIALS: PAGE_MY_MATERIALS,
  MY_MATERIAL_DETAIL: PAGE_MY_MATERIAL_DETAIL,
  MY_MATERIAL_EDIT: PAGE_MY_MATERIAL_EDIT,
  MY_BULK_MATERIALS: PAGE_MY_BULK_MATERIALS,
  MY_BULK_MATERIAL_DETAIL: PAGE_MY_BULK_MATERIAL_DETAIL,
  MY_BULK_MATERIAL_EDIT: PAGE_MY_BULK_MATERIAL_EDIT,
  MY_DEMAND_LIST: PAGE_MY_DEMAND_LIST,
  MY_DEMAND_DETAIL: PAGE_MY_DEMAND_DETAIL,
  MY_DEMAND_EDIT: PAGE_MY_DEMAND_EDIT,
  MY_GROUP_ORDERS: PAGE_MY_GROUP_ORDERS,
  MY_GROUP_DETAIL: PAGE_MY_GROUP_DETAIL,
  MY_GROUP_EDIT: PAGE_MY_GROUP_EDIT,
  MY_PROCESSING_DETAIL: PAGE_MY_PROCESSING_DETAIL,
  MY_PROCESSING_EDIT: PAGE_MY_PROCESSING_EDIT,

  // 其他功能
  MESSAGES: PAGE_MESSAGES,
  MESSAGE_DETAIL: PAGE_MESSAGE_DETAIL,
  NOTIFICATIONS: PAGE_NOTIFICATIONS,
  MY_MESSAGES: PAGE_MY_MESSAGES,
  MY_INQUIRIES: PAGE_MY_INQUIRIES,
  MY_PROCESSING: PAGE_MY_PROCESSING,
  MY_ORDERS: PAGE_MY_ORDERS,
  SETTINGS: PAGE_SETTINGS,
  PROFILE_DETAIL: PAGE_PROFILE_DETAIL,
  ACCOUNT_MANAGEMENT: PAGE_ACCOUNT_MANAGEMENT,
  ENTERPRISE_MANAGEMENT: PAGE_ENTERPRISE_MANAGEMENT,
  EDIT_AVATAR: PAGE_EDIT_AVATAR,
  EDIT_NAME: PAGE_EDIT_NAME,
  EDIT_PHONE: PAGE_EDIT_PHONE,
  RECEIVED_ORDERS: PAGE_RECEIVED_ORDERS,
  COLLECTIONS: PAGE_COLLECTIONS,
  INVITATIONS: PAGE_INVITATIONS,
  INVITE_SHARE: PAGE_INVITE_SHARE,
  POINTS_MALL: PAGE_PRIZES,
  CONSULTATION: PAGE_CONSULTATION,
  MATERIAL_DETAIL: PAGE_MATERIAL_DETAIL,
  PROCESSING_DETAIL: PAGE_PROCESSING_DETAIL,

  // 积分商城相关
  INTEGRAL_MALL: PAGE_INTEGRAL_MALL,
  INTEGRAL_DETAIL: PAGE_INTEGRAL_DETAIL,
  INTEGRAL_ORDER_CONFIRM: PAGE_INTEGRAL_ORDER_CONFIRM,
  INTEGRAL_ORDERS: PAGE_INTEGRAL_ORDERS,
  INTEGRAL_ORDER_DETAIL: PAGE_INTEGRAL_ORDER_DETAIL,
  INTEGRAL_ORDER_EXPRESS: PAGE_INTEGRAL_ORDER_EXPRESS,
  MY_EXCHANGES: PAGE_MY_EXCHANGES,

  // 法律和协议
  DISCLAIMER: PAGE_DISCLAIMER,
  PRIVACY_POLICY: PAGE_PRIVACY_POLICY,
  HELP: PAGE_HELP
}

// ==================== 需要登录的页面列表 ====================

// 不需要登录的公开页面（白名单）
const PUBLIC_PAGES = [
  PAGE_HOME,              // 首页
  PAGE_LOGIN,             // 登录页
  PAGE_PHONE_VERIFICATION, // 手机验证页
  PAGE_DISCLAIMER,        // 免责声明
  PAGE_PRIVACY_POLICY,     // 隐私政策
  PAGE_HELP,              // 使用说明
  PAGE_DEMAND_LIST,       // 需求列表页
];

// 所有页面列表
const ALL_PAGES = [
  // 主要页面
  PAGE_HOME,
  PAGE_INDEX,
  PAGE_LOGS,
  PAGE_PROFILE,
  PAGE_LOGIN,
  PAGE_PHONE_VERIFICATION,
  
  // 发布相关
  PAGE_PUBLISH_SELECT,
  PAGE_PUBLISH_DEMAND,
  PAGE_PUBLISH_GROUP,
  PAGE_PUBLISH_INVENTORY,
  PAGE_PUBLISH_MATERIAL,
  PAGE_PUBLISH_PROCESSING,
  
  // 企业相关
  PAGE_CREATE_ENTERPRISE,
  PAGE_MY_ENTERPRISE,
  PAGE_EDIT_COMPANY,
  PAGE_MEMBERSHIP_REGISTER,
  PAGE_COMPANY_PROFILE,
  PAGE_ENTERPRISE_PRODUCT,
  
  // 产品相关
  PAGE_PRODUCT_HOME,
  PAGE_PRODUCT_LIST_MATERIAL,
  PAGE_PRODUCT_LIST_PROCESSING,
  PAGE_PRODUCT_DETAIL,
  
  // 散料相关
  PAGE_BULK_MATERIALS_HOME,
  PAGE_BULK_MATERIALS,
  PAGE_BULK_MATERIAL_DETAIL,
  PAGE_BULK_MATERIALS_CATEGORY,
  PAGE_BULK_PROCESSING_CATEGORY,
  PAGE_BULK_INVENTORY_CATEGORY,
  
  // 搜索相关
  PAGE_SEARCH_RESULTS,
  PAGE_LOOK_FOR_TECH_PARAMS,
  
  // 需求相关
  PAGE_DEMAND_LIST,
  PAGE_DEMAND_DETAIL,
  PAGE_GROUP_LIST,
  PAGE_GROUP_DETAIL,
  
  // 我的相关
  PAGE_MY_POINTS,
  PAGE_POINTS_RULES,
  PAGE_PRIZES,
  PAGE_MY_PRIZES,
  PAGE_MY_MATERIALS,
  PAGE_MY_MATERIAL_DETAIL,
  PAGE_MY_MATERIAL_EDIT,
  PAGE_MY_BULK_MATERIALS,
  PAGE_MY_BULK_MATERIAL_DETAIL,
  PAGE_MY_BULK_MATERIAL_EDIT,
  PAGE_MY_DEMAND_LIST,
  PAGE_MY_DEMAND_DETAIL,
  PAGE_MY_DEMAND_EDIT,
  PAGE_MY_GROUP_ORDERS,
  PAGE_MY_GROUP_DETAIL,
  PAGE_MY_GROUP_EDIT,
  PAGE_MY_PROCESSING_DETAIL,
  PAGE_MY_PROCESSING_EDIT,
  
  // 其他功能
  PAGE_MESSAGES,
  PAGE_NOTIFICATIONS,
  PAGE_MY_MESSAGES,
  PAGE_MY_INQUIRIES,
  PAGE_MY_PROCESSING,
  PAGE_MY_ORDERS,
  PAGE_SETTINGS,
  PAGE_PROFILE_DETAIL,
  PAGE_ACCOUNT_MANAGEMENT,
  PAGE_ENTERPRISE_MANAGEMENT,
  PAGE_EDIT_AVATAR,
  PAGE_EDIT_NAME,
  PAGE_EDIT_PHONE,
  PAGE_RECEIVED_ORDERS,
  PAGE_COLLECTIONS,
  PAGE_INVITATIONS,
  PAGE_INVITE_SHARE,
  PAGE_CONSULTATION,
  PAGE_MATERIAL_DETAIL,
  PAGE_PROCESSING_DETAIL,
  
  // 法律和协议
  PAGE_DISCLAIMER,
  PAGE_PRIVACY_POLICY,
  PAGE_HELP
];

// 需要登录才能访问的页面（除公开页面外的所有页面）
const LOGIN_REQUIRED_PAGES = ALL_PAGES.filter(page => !PUBLIC_PAGES.includes(page));

// ==================== 页面跳转工具函数 ====================

/**
 * 检查页面是否需要登录
 * @param {string} url 页面路径
 * @returns {boolean} 是否需要登录
 */
const isLoginRequired = (url) => {
  return LOGIN_REQUIRED_PAGES.includes(url);
};

/**
 * 页面跳转工具函数（带登录检测）
 * @param {string} url 页面路径
 * @param {object} params 页面参数
 * @param {string} method 跳转方法 'navigateTo' | 'redirectTo' | 'switchTab' | 'reLaunch'
 * @param {boolean} skipLoginCheck 是否跳过登录检测
 */
export const navigateToPage = (url, params = {}, method = 'navigateTo', skipLoginCheck = false) => {
  // 检查是否需要登录
  if (!skipLoginCheck && isLoginRequired(url)) {
    if (!checkLogin()) {
      console.log('页面需要登录，跳转到登录页:', url);
      toLogin();
      return;
    }
  }

  console.log('跳转到页面:', url);
  console.log('页面参数:', params);
  console.log('跳转方法:', method);
  console.log('跳过登录检测:', skipLoginCheck);

  let fullUrl = url
  
  // 处理参数
  if (Object.keys(params).length > 0) {
    const queryString = Object.keys(params)
      .map(key => `${key}=${encodeURIComponent(params[key])}`)
      .join('&')
    fullUrl += `?${queryString}`
  }

  // 根据方法跳转
  switch (method) {
    case 'navigateTo':
      wx.navigateTo({ url: fullUrl })
      break
    case 'redirectTo':
      wx.redirectTo({ url: fullUrl })
      break
    case 'switchTab':
      wx.switchTab({ url: fullUrl })
      break
    case 'reLaunch':
      wx.reLaunch({ url: fullUrl })
      break
    default:
      wx.navigateTo({ url: fullUrl })
  }
}

/**
 * 带参数的页面跳转（推荐使用）
 * @param {string} pagePath 页面路径常量
 * @param {object} params 页面参数
 * @param {string} method 跳转方法
 * @param {boolean} skipLoginCheck 是否跳过登录检测
 */
export const goToPage = (pagePath, params = {}, method = 'navigateTo', skipLoginCheck = false) => {
  navigateToPage(pagePath, params, method, skipLoginCheck)
}

/**
 * 跳转到需要登录的页面（自动检测登录状态）
 * @param {string} pagePath 页面路径常量
 * @param {object} params 页面参数
 * @param {string} method 跳转方法
 */
export const goToLoginRequiredPage = (pagePath, params = {}, method = 'navigateTo') => {
  goToPage(pagePath, params, method, false)
}

/**
 * 跳转到公开页面（跳过登录检测）
 * @param {string} pagePath 页面路径常量
 * @param {object} params 页面参数
 * @param {string} method 跳转方法
 */
export const goToPublicPage = (pagePath, params = {}, method = 'navigateTo') => {
  goToPage(pagePath, params, method, true)
}

/**
 * 返回上一页
 */
export const goBack = () => {
  wx.navigateBack()
}

/**
 * 返回首页
 */
export const goHome = () => {
  wx.switchTab({ url: PAGE_HOME })
}

/**
 * 检查登录状态并跳转到指定页面
 * @param {string} pagePath 页面路径常量
 * @param {object} params 页面参数
 * @param {string} method 跳转方法
 */
export const checkLoginAndGo = (pagePath, params = {}, method = 'navigateTo') => {
  if (checkLogin()) {
    goToPage(pagePath, params, method, true)
  } else {
    toLogin()
  }
}

/**
 * 登录成功后跳转到指定页面
 * @param {string} pagePath 页面路径常量
 * @param {object} params 页面参数
 * @param {string} method 跳转方法
 */
export const loginAndGo = (pagePath, params = {}, method = 'navigateTo') => {
  // 保存目标页面，登录成功后跳转
  wx.setStorageSync('login_target_page', {
    path: pagePath,
    params,
    method
  })
  toLogin()
}

/**
 * 处理登录成功后的页面跳转
 */
export const handleLoginSuccess = () => {
  const targetPage = wx.getStorageSync('login_target_page')
  if (targetPage) {
    wx.removeStorageSync('login_target_page')
    goToPage(targetPage.path, targetPage.params, targetPage.method, true)
  } else {
    goHome()
  }
}
